package com.cj.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisCacheService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 用户信息缓存
    public void cacheUserInfo(Long userId, Object userInfo) {
        String key = "user:" + userId;
        redisTemplate.opsForValue().set(key, userInfo, 1, TimeUnit.HOURS);
    }

    // 获取缓存的用户信息
    public Object getCachedUserInfo(Long userId) {
        String key = "user:" + userId;
        return redisTemplate.opsForValue().get(key);
    }

    // 文章状态缓存
    public void cacheArticleState(Long articleId, String state) {
        String key = "article:state:" + articleId;
        redisTemplate.opsForValue().set(key, state);
    }

    // 点赞数缓存
    public Long incrementLikeCount(Long commentId) {
        String key = "comment:like:" + commentId;
        return redisTemplate.opsForValue().increment(key);
    }

    // 删除缓存
    public void deleteCache(String key) {
        redisTemplate.delete(key);
    }
}
